package com.terracloud.server.utils;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import com.terracloud.common.util.ListeningExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 *
 * @author chenhainan
 * @since 2025/9/17
 */
//@Component
public class SimpleListeningExecutor implements ListeningExecutor {

    private ListeningExecutorService service;

    @PostConstruct
    public void init() {
        this.service = MoreExecutors.listeningDecorator(Executors.newVirtualThreadPerTaskExecutor());
    }

    @PreDestroy
    public void destroy() {
        if (this.service != null) {
            this.service.shutdown();
        }
    }

    @Override
    public <T> ListenableFuture<T> executeAsync(Callable<T> task) {
        return service.submit(task);
    }

    @Override
    public void execute(@NotNull Runnable command) {
        service.execute(command);
    }
}
